﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.UI;
using UnityEngine;
using Assets.Scripts.TDu3DWork;
using Assets.Scripts.TDuResource;

namespace Assets.Scripts.TDuVRUI
{
    class TDuMainNavigator
    {
        public List<GameObject> m_listBigBtns;
        public List<GameObject> m_listSmlScrollViews;
        public int m_nCurBigStep;
        public int m_nCurSmlStep;

        private DefaultControls.Resources m_uiResources;

        private GameObject firstPanel;
        private GameObject secondPanel;
        private GameObject thirdPanel;
        private GameObject title;
        private GameObject bigStepScrollView;
        private GameObject promptText;

        public TDuMainNavigator( )
        {
            m_nCurBigStep = 1;
            m_nCurSmlStep = 0;
            m_listBigBtns = new List<GameObject>();
            m_listSmlScrollViews = new List<GameObject>();

            m_uiResources = new DefaultControls.Resources();
        }
        public void create(TDuSceneBaseWorker baseWorker, GameObject parent)
        {
            float fFirstRowRatio = 0.1f;
            float fSecondRowRatio = 0.7f;
            float fThirdRowRatio = 0.2f;

            secondPanel = GameObject.Find("SecondPanel");
            thirdPanel = GameObject.Find("ThirdPanel");
            //添加垂直布局器;


            //第一个面板建立标题;


            //第二个面板建立大小步骤滚动试图;

            GameObject content = GameObject.Find("/MainCanvas/SecondPanel/BigScrollView/Viewport/Content");

            //滚动试图建立列表;
            for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps.Count; i++)
            {
                GameObject uiButton =GameObject.Instantiate( Resources.Load<GameObject>("Prefab/步骤_Prefab"));
                uiButton.transform.SetParent(content.transform, false);
                uiButton.name = baseWorker.m_sceneCfg.vecOperSteps[i].strBigStep;
                //uiButton.GetComponent<Button>().image.sprite = Resources.Load<Sprite>("preImage/unchoiceBig");
                uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().text = baseWorker.m_sceneCfg.vecOperSteps[i].strBigStep;
                uiButton.GetComponent<Button>().onClick.AddListener(
                    () =>
                    {
                        onBigBtnClick(uiButton.name, baseWorker);
                    }
                );

                m_listBigBtns.Add(uiButton);
            }

            for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps.Count; i++)
            {
                GameObject smlStepScrollView = GameObject.Instantiate(Resources.Load<GameObject>("Prefab/SmallScrollView"));
                smlStepScrollView.name = "SmallScrollView" + i.ToString();
                smlStepScrollView.transform.SetParent(secondPanel.transform, false);

                m_listSmlScrollViews.Add(smlStepScrollView);

                GameObject smallContent = GameObject.Find("/MainCanvas/SecondPanel/" + smlStepScrollView.name + "/Viewport/Content");
                //滚动试图建立列表;
                for (int index = 0; index < baseWorker.m_sceneCfg.vecOperSteps[i].arrSmlSteps.Count; index++)
                {
                    GameObject uiButton = smallContent.transform.GetChild(0).gameObject;
                    uiButton.name = baseWorker.m_sceneCfg.vecOperSteps[i].arrSmlSteps[index];

                    uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().text = baseWorker.m_sceneCfg.vecOperSteps[i].arrSmlSteps[index];
                    uiButton.GetComponent<Button>().onClick.AddListener(
                        () =>
                        {
                            onSmlBtnClk(uiButton.name, baseWorker);
                        }
                    );
                }
                if (i != 0)
                {
                    m_listSmlScrollViews[i].SetActive(false);
                    //smallContent.SetActive(false);
                }

            }


            //第三个面板建立文字提示;
            promptText = thirdPanel.transform.Find("Text").gameObject;

            Text promptTextComponent = promptText.GetComponent<Text>();
            promptTextComponent.text = "<步骤提示文字>";
        }


        //public void create(TDuSceneBaseWorker baseWorker, GameObject parent )
        //{
        //    float fFirstRowRatio = 0.1f;
        //    float fSecondRowRatio = 0.7f;
        //    float fThirdRowRatio = 0.2f;

        //    //添加垂直布局器;

        //    VerticalLayoutGroup verticalLayout = parent.AddComponent<VerticalLayoutGroup>();
        //    verticalLayout.childControlHeight = false;
        //    verticalLayout.childControlWidth = false;
        //    verticalLayout.SetLayoutVertical();

        //    Vector2 rcCanvas = parent.GetComponent<RectTransform>().sizeDelta;

        //    firstPanel = DefaultControls.CreatePanel(m_uiResources);
        //    firstPanel.layer = parent.layer;
        //    firstPanel.transform.SetParent(parent.transform, false);
        //    firstPanel.GetComponent<RectTransform>().sizeDelta = new Vector2(rcCanvas.x, rcCanvas.y * fFirstRowRatio);

        //    secondPanel = DefaultControls.CreatePanel(m_uiResources);
        //    secondPanel.layer = parent.layer;
        //    secondPanel.transform.SetParent(parent.transform, false);
        //    secondPanel.GetComponent<RectTransform>().sizeDelta = new Vector2(rcCanvas.x, rcCanvas.y * fSecondRowRatio);

        //    thirdPanel = DefaultControls.CreatePanel(m_uiResources);
        //    thirdPanel.layer = parent.layer;
        //    thirdPanel.transform.SetParent(parent.transform, false);
        //    thirdPanel.GetComponent<RectTransform>().sizeDelta = new Vector2(rcCanvas.x, rcCanvas.y * fThirdRowRatio);

        //    //第一个面板建立标题;
        //    firstPanel.AddComponent<HorizontalLayoutGroup>();
        //    GameObject title = DefaultControls.CreateText(m_uiResources);
        //    title.transform.SetParent(firstPanel.transform, false);
        //    title.layer = parent.layer;
        //    title.GetComponent<RectTransform>().sizeDelta = new Vector2(rcCanvas.x, rcCanvas.y * fFirstRowRatio);
        //    Text textComponent = title.GetComponent<Text>();
        //    textComponent.text = "步骤导航";
        //    textComponent.fontSize = 20;
        //    textComponent.fontStyle = FontStyle.Bold;
        //    textComponent.alignment = TextAnchor.MiddleCenter;

        //    //第二个面板建立大小步骤滚动试图;
        //    secondPanel.AddComponent<HorizontalLayoutGroup>();
        //    bigStepScrollView = DefaultControls.CreateScrollView(m_uiResources);
        //    bigStepScrollView.name = "BigScrollView";
        //    bigStepScrollView.layer = parent.layer;
        //    bigStepScrollView.transform.SetParent(secondPanel.transform, false);
        //    bigStepScrollView.GetComponent<RectTransform>().sizeDelta = new Vector2(rcCanvas.x / 2, rcCanvas.y * fSecondRowRatio);

        //    GameObject content = GameObject.Find("/MainCanvas/Panel/BigScrollView/Viewport/Content");
        //    content.AddComponent<VerticalLayoutGroup>();
        //    //滚动试图建立列表;
        //    for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps.Count; i++)
        //    {
        //        GameObject uiButton = DefaultControls.CreateButton(m_uiResources);
        //        uiButton.layer = parent.layer;
        //        uiButton.transform.SetParent(content.transform, false);
        //        uiButton.name = baseWorker.m_sceneCfg.vecOperSteps[i].strBigStep;
        //        uiButton.GetComponent<Button>().image.sprite = Resources.Load<Sprite>("preImage/unchoiceBig");

        //        uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().text = baseWorker.m_sceneCfg.vecOperSteps[i].strBigStep;
        //        uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().color = Color.black;
        //        uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().fontSize = 14;
        //        uiButton.GetComponent<Button>().onClick.AddListener(
        //            () =>
        //            {
        //                onBigBtnClick(uiButton.name, baseWorker);
        //            }
        //        );

        //        m_listBigBtns.Add(uiButton);
        //    }

        //    for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps.Count; i++)
        //    {
        //        GameObject smlStepScrollView = DefaultControls.CreateScrollView(m_uiResources);
        //        smlStepScrollView.name = "SmallScrollView" + i.ToString();
        //        smlStepScrollView.layer = parent.layer;
        //        smlStepScrollView.transform.SetParent(secondPanel.transform, false);
        //        smlStepScrollView.GetComponent<RectTransform>().sizeDelta = new Vector2(rcCanvas.x / 2, rcCanvas.y * fSecondRowRatio);
        //        m_listSmlScrollViews.Add(smlStepScrollView);

        //        GameObject smallContent = GameObject.Find("/MainCanvas/Panel/"+smlStepScrollView.name+"/Viewport/Content");
        //        smallContent.AddComponent<VerticalLayoutGroup>();
        //        //滚动试图建立列表;
        //        for( int index = 0; index < baseWorker.m_sceneCfg.vecOperSteps[i].arrSmlSteps.Count; index++)
        //        {
        //            GameObject uiButton = DefaultControls.CreateButton(m_uiResources);
        //            uiButton.transform.SetParent(smallContent.transform, false);
        //            uiButton.name = baseWorker.m_sceneCfg.vecOperSteps[i].arrSmlSteps[index];
        //            uiButton.layer = parent.layer;
        //            uiButton.GetComponent<Button>().image.sprite = Resources.Load<Sprite>("preImage/unchoiceBig");

        //            uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().text = baseWorker.m_sceneCfg.vecOperSteps[i].arrSmlSteps[index];
        //            uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().color = Color.black;
        //            uiButton.transform.Find("Text").GetComponent<Text>().GetComponent<Text>().fontSize = 14;
        //            uiButton.GetComponent<Button>().onClick.AddListener(
        //                () =>
        //                {
        //                    onSmlBtnClk(uiButton.name, baseWorker);
        //                }
        //            );
        //        }
        //        if( i != 0)
        //        {
        //            m_listSmlScrollViews[i].SetActive(false);
        //            smallContent.SetActive(false);
        //        }

        //    }


        //    //第三个面板建立文字提示;
        //    thirdPanel.AddComponent<HorizontalLayoutGroup>();
        //    promptText = DefaultControls.CreateText(m_uiResources);
        //    promptText.layer = parent.layer;
        //    promptText.transform.SetParent(thirdPanel.transform, false);
        //    promptText.GetComponent<RectTransform>().sizeDelta = new Vector2(rcCanvas.x, rcCanvas.y * fThirdRowRatio);

        //    Text promptTextComponent = promptText.GetComponent<Text>();
        //    promptTextComponent.text = "<步骤提示文字>";
        //    promptTextComponent.fontSize = 20;
        //    promptTextComponent.alignment = TextAnchor.MiddleCenter;
        //}

        public void destroy()
        {
            
        }

        // 大步骤事件处理
        public void onBigBtnClick(string strBtnName, TDuSceneBaseWorker baseWorker)
        {
            for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps.Count; i++)
            {
                m_listSmlScrollViews[i].SetActive(false);
                //GameObject.Find("/MainCanvas/SecondPanel/BigScrollView/Viewport/Content/" + baseWorker.m_sceneCfg.vecOperSteps[i].strBigStep).GetComponent<Image>().sprite = Resources.Load<Sprite>("preImage/unchoiceSmall");
                if (baseWorker.m_sceneCfg.vecOperSteps[i].strBigStep == strBtnName)
                {
                    m_nCurBigStep = i + 1;
                    Debug.Log(m_nCurBigStep);

                    m_listSmlScrollViews[i].SetActive(true);
                    string strSmlStepScrollView = "SmallScrollView" + i.ToString();
                    //GameObject smallContent = GameObject.Find("/MainCanvas/Panel/" + strSmlStepScrollView + "/Viewport/Content");
                    //smallContent.SetActive(true);

                    //GameObject.Find("/MainCanvas/SecondPanel/BigScrollView/Viewport/Content/" + baseWorker.m_sceneCfg.vecOperSteps[i].strBigStep).GetComponent<Image>().sprite = Resources.Load<Sprite>("preImage/choiceSmall");
                }

            }
            for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps.Count; i++)
            {
                if (baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i].IndexOf('/') != -1)
                {
                    GameObject.Find("/MainCanvas/SecondPanel/SmallScrollView/Viewport/Content/" + baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i].Replace('/', '-')).transform.Find("Text").GetComponent<Text>().color = Color.white;
                }
                else
                    GameObject.Find("/MainCanvas/SecondPanel/SmallScrollView/Viewport/Content/" + baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i]).transform.Find("Text").GetComponent<Text>().color = Color.white;
            }
        }

        // 小步骤事件处理
        public void onSmlBtnClk(string strSmlBtnName, TDuSceneBaseWorker baseWorker)
        {
            string strCurStep = "";

            string strSmallScrollViewName = "SmallScrollView" + (m_nCurBigStep-1).ToString();
            for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps.Count; i++)
            {
                if (baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i].IndexOf('/') != -1)
                {
                    GameObject.Find("/MainCanvas/SecondPanel/" + strSmallScrollViewName + "/Viewport/Content/" + baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i].Replace('/', '-')); ;
                }
                else
                {
                    GameObject.Find("/MainCanvas/SecondPanel/" + strSmallScrollViewName + "/Viewport/Content/" + baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i]) ;
                }
            }

            Debug.Log("------------------");
            //获得当前小步骤面板的步骤
            for (int i = 0; i < baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps.Count; i++)
            {
                if (baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i].IndexOf('/') != -1)
                {
                    if (baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i].Replace('/', '-') == strSmlBtnName)
                    {
                        m_nCurSmlStep = i + 1;
                        strCurStep = m_nCurBigStep.ToString() + "0" + m_nCurSmlStep.ToString() + "-";

                        Debug.Log("当前小步骤:" + strCurStep + strSmlBtnName);
                        //GameObject.Find("/MainCanvas/Panel/" + strSmallScrollViewName + "/Viewport/Content/" + strSmlBtnName).GetComponent<Button>().image.sprite = Resources.Load<Sprite>("preImage/choiceSmall");
                        break;
                    }
                }
                else
                {
                    if (baseWorker.m_sceneCfg.vecOperSteps[m_nCurBigStep - 1].arrSmlSteps[i] == strSmlBtnName)
                    {
                        m_nCurSmlStep = i + 1;
                        strCurStep = m_nCurBigStep.ToString() + "0" + m_nCurSmlStep.ToString() + "-";

                        Debug.Log("当前小步骤1:" + strCurStep + strSmlBtnName);
                        //GameObject.Find("/MainCanvas/Panel/" + strSmallScrollViewName + "/Viewport/Content/" + strSmlBtnName).GetComponent<Button>().image.sprite = Resources.Load<Sprite>("preImage/choiceSmall");
                        break;
                    }
                }
            }

            string strExmStepName = strCurStep + strSmlBtnName;
            if (baseWorker.m_sceneCfg.vecVariableNode.ContainsKey(strExmStepName))
            {
                TDuPostEventArgs postArgs = new TDuPostEventArgs();
                postArgs.getPostActionType(baseWorker.m_sceneCfg.vecVariableNode[strExmStepName].strVarNodeType);
                postArgs.m_varNode = (baseWorker.m_sceneCfg.vecVariableNode[strExmStepName]);
                baseWorker.onPostAction(postArgs);
            }
        }

        public void updateTipText( string strTipText)
        {
            Debug.Log("更新提示文字");
            Text promptTextComponent = promptText.GetComponent<Text>();
            promptTextComponent.text = strTipText;
        }
    }
}
